package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectLand;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.*;
import com.ruoyi.industryfrontend.service.IProjectLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 土地Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@Service
public class ProjectLandServiceImpl implements IProjectLandService
{
    @Autowired
    private ProjectLandMapper projectLandMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询土地
     *
     * @param id 土地主键
     * @return 土地
     */
    @Override
    public ProjectLand selectProjectLandById(Long id)
    {
        return projectLandMapper.selectProjectLandById(id);
    }

    /**
     * 查询土地
     * @param spaceId 编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectProjectLandByNumber(String spaceId) {
        ProjectLand projectLand = projectLandMapper.selectProjectLandByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectLand.setCollectState(1L);
        }else {
            projectLand.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectLand);
        if (projectLand.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectLand.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询土地列表
     *
     * @param projectLand 土地
     * @return 土地
     */
    @Override
    public List<ProjectLand> selectProjectLandList(ProjectLand projectLand)
    {
//        //所属城市
//        List<Long> districtIds = new ArrayList<Long>();
//        if (projectLand.getCityId()!=null){
//            districtIds.add(projectLand.getCityId());
//            projectLand.setCityIds(districtIds);
//        }else if (projectLand.getCityId()==null && projectLand.getProvinceId() != null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectLand.getProvinceId()));
//            districtIds.add(projectLand.getProvinceId());
//            projectLand.setCityIds(districtIds);
//        }
        projectLand.setSupplyUserId(SecurityUtils.getUserId());
        return projectLandMapper.selectProjectLandListz(projectLand);
    }

    /**
     * 新增土地
     *
     * @param projectLand 土地
     * @return 结果
     */
    @Override
    public int insertProjectLand(ProjectLand projectLand)
    {
        projectLand.setSupplyUserId(SecurityUtils.getUserId());
        projectLand.setApprovalState(0L);
        projectLand.setCreateTime(DateUtils.getNowDate());
        projectLand.setSerialNumber(IdUtils.fastSimpleUUID());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectLand, spaceIndustry);
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectLand.getCreateTime());
        spaceIndustry.setSpaceId(projectLand.getSerialNumber());
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
        return projectLandMapper.insertProjectLand(projectLand);
    }

    private static void extracted(ProjectLand projectLand, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(8);
        spaceIndustry.setReleaseType(0L);
        spaceIndustry.setApprovalState(projectLand.getApprovalState());
        spaceIndustry.setProvinceId(projectLand.getProvinceId());//省份
        spaceIndustry.setCityId(projectLand.getCityId());//城市
        spaceIndustry.setAreaId(projectLand.getAreaId());//区域
        spaceIndustry.setParkId(String.valueOf(projectLand.getParkId()));
        spaceIndustry.setLeasePrice(projectLand.getLandPrice());//出售价格
        spaceIndustry.setSellPrice(projectLand.getLandPrice());//租赁价格
        spaceIndustry.setCoverResource(projectLand.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectLand.getLandName());//园区名称
        spaceIndustry.setSpaceAllSize(projectLand.getLandAllSize());//空间总面积
        spaceIndustry.setTop(projectLand.getTop());//置顶
    }

    /**
     * 修改土地
     *
     * @param projectLand 土地
     * @return 结果
     */
    @Override
    public int updateProjectLand(ProjectLand projectLand)
    {
        projectLand.setApprovalState(0L);//未审核
        projectLand.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectLand,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectLand.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectLand.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByLandId(spaceIndustry);
        return projectLandMapper.updateProjectLand(projectLand);
    }

    /**
     * 批量删除土地
     *
     * @param ids 需要删除的土地主键
     * @return 结果
     */
    @Override
    public int deleteProjectLandByIds(Long[] ids)
    {
        return projectLandMapper.deleteProjectLandByIds(ids);
    }

    /**
     * 删除土地信息
     *
     * @param id 土地主键
     * @return 结果
     */
    @Override
    public int deleteProjectLandById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectLandMapper.deleteProjectLandById(id);
    }

    @Override
    public int deleteProjectLandBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectLandMapper.deleteProjectLandBySerialNumber(spaceId);
    }
}
